package com.qmth.gkwb.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qmth.gkwb.data.entity.DictCity;
import com.qmth.gkwb.data.entity.DictExamType;
import com.qmth.gkwb.data.entity.DictForeign;
import com.qmth.gkwb.data.entity.DictGraduation;
import com.qmth.gkwb.data.entity.DictNation;
import com.qmth.gkwb.data.entity.DictPolity;
import com.qmth.gkwb.data.entity.DictProvince;
import com.qmth.gkwb.data.entity.DictRegion;
import com.qmth.gkwb.data.entity.DictSignPoint;
import com.qmth.gkwb.data.entity.DictSpecDirect;
import com.qmth.gkwb.data.entity.DictSpecialty;
import com.qmth.gkwb.data.entity.DictSport;
import com.qmth.gkwb.data.entity.DictStuType;
import com.qmth.gkwb.data.entity.DictSubject;
import com.qmth.gkwb.data.entity.School;
import com.qmth.gkwb.data.repository.DictCityRepo;
import com.qmth.gkwb.data.repository.DictExamTypeRepo;
import com.qmth.gkwb.data.repository.DictForeignRepo;
import com.qmth.gkwb.data.repository.DictGraduationRepo;
import com.qmth.gkwb.data.repository.DictNationRepo;
import com.qmth.gkwb.data.repository.DictPolityRepo;
import com.qmth.gkwb.data.repository.DictProvRepo;
import com.qmth.gkwb.data.repository.DictRegionRepo;
import com.qmth.gkwb.data.repository.DictSignPointRepo;
import com.qmth.gkwb.data.repository.DictSpecDirectRepo;
import com.qmth.gkwb.data.repository.DictSpecialtyRepo;
import com.qmth.gkwb.data.repository.DictSportRepo;
import com.qmth.gkwb.data.repository.DictStuTypeRepo;
import com.qmth.gkwb.data.repository.DictSubjectRepo;
import com.qmth.gkwb.data.repository.SchoolRepo;

/**
 * 数据字典data rest服务
 * @author zhengmin
 *
 */
@RestController
@RequestMapping("api/dict")
public class DictApi {

	@Autowired private DictSpecialtyRepo specRepo;
	@Autowired private DictExamTypeRepo examTypeRepo;
	@Autowired private DictSubjectRepo subjectRepo;
	@Autowired private DictSpecDirectRepo specDirectRepo;
	@Autowired private DictProvRepo provRepo;
	@Autowired private DictCityRepo cityRepo;
	@Autowired private DictSportRepo sportRepo;
	@Autowired private DictRegionRepo regionRepo;
	@Autowired private DictSignPointRepo signPointRepo;
	@Autowired private SchoolRepo schoolRepo;
	@Autowired private DictNationRepo nationRepo;
	@Autowired private DictForeignRepo foreignRepo;
	@Autowired private DictStuTypeRepo stuTypeRepo;
	@Autowired private DictPolityRepo polityRepo;
	@Autowired private DictGraduationRepo graduationRepo;
	
	//民族
	@RequestMapping(value="nations",method=RequestMethod.GET)
	public List<DictNation> queryNations(){
		return nationRepo.findAll(new Sort("order"));
	}
	
	//外语语种
	@RequestMapping(value="foreigns",method=RequestMethod.GET)
	public List<DictForeign> queryForeigns(){
		return foreignRepo.findAll();
	}
	
	//毕业类别
	@RequestMapping(value="graduations",method=RequestMethod.GET)
	public List<DictGraduation> queryGraduations(){
		return graduationRepo.findAll();
	}
	
	//政治面貌
	@RequestMapping(value="polities",method=RequestMethod.GET)
	public List<DictPolity> queryPolities(){
		return polityRepo.findAll(new Sort("order"));
	}
	
	//考生类型
	@RequestMapping(value="stutypes",method=RequestMethod.GET)
	public List<DictStuType> queryStuTypes(){
		return stuTypeRepo.findAll(new Sort("order"));
	}
	
	//专业
	
	@RequestMapping(value="specialties",method=RequestMethod.GET)
	public List<DictSpecialty> querySpecialties(){
		return specRepo.findAll(new Sort("idx"));
	}
	
	//专业方向
	@RequestMapping(value="specialties/{id}/direct",method=RequestMethod.GET)
	public List<DictSpecDirect> querySpecDirect(@PathVariable long id){
		return specDirectRepo.findBySpecId(id);
	}
	
	//省份
	@RequestMapping(value="provs",method=RequestMethod.GET)
	public List<DictProvince> queryProvs(){
		return provRepo.findAll(new Sort("order"));
	}
	
	/**
	 * 市州
	 * @param provId 省份id
	 * @return
	 */
	@RequestMapping(value="cities",method=RequestMethod.GET)
	public List<DictCity> queryCities(@RequestParam long provId){
		return cityRepo.findByProvId(provId,new Sort("no"));
	}
	
	@RequestMapping(value="cities/{id}",method=RequestMethod.GET)
	public DictCity getCityById(@PathVariable long id){
		return cityRepo.findOne(id);
	}
	
	/**
	 * 区县
	 * @param cityId 市州id
	 * @return
	 */
	@RequestMapping(value="regions",method=RequestMethod.GET)
	public List<DictRegion> queryRegions(@RequestParam long cityId){
		return regionRepo.findByCityId(cityId,new Sort("order"));
	}
	
	@RequestMapping(value="regions/{id}",method=RequestMethod.GET)
	public DictRegion getRegionById(@PathVariable long id){
		return regionRepo.findOne(id);
	}
	
	@RequestMapping(value="sports",method=RequestMethod.GET)
	public List<DictSport> querySports(){
		return sportRepo.findAll();
	}
	
	/**
	 * 报名点
	 * @param id
	 * @return
	 */
	@RequestMapping(value="signpoints/{id}",method=RequestMethod.GET)
	public DictSignPoint getSignPointById(@PathVariable long id){
		return signPointRepo.findOne(id);
	}
	
	//考试类型
	@RequestMapping(value="examTypes/{id}",method=RequestMethod.GET)
	public DictExamType getExamTypeById(@PathVariable long id){
		return examTypeRepo.findOne(id);
	}
	
	//科类
	@RequestMapping(value="subjects/{id}",method=RequestMethod.GET)
	public DictSubject getSubjectById(@PathVariable long id){
		return subjectRepo.findOne(id);
	}
	
	//毕业学校
	@RequestMapping(value="schools",method=RequestMethod.GET)
	public List<School> getSchoolByRegionId(@RequestParam long regionId){
		return schoolRepo.findByRegionId(regionId);
	}
}
